package com.dss.service;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;

import com.dss.dao.RescueTecDAO;
import com.dss.po.RescueTec;
import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.enums.RoleType;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.FileUpload;
import com.infomanage.util.StringUtil;
import com.infomanage.util.TimeUtil;
import com.infomanage.util.smartupload.SmartRequest;
import com.infomanage.util.smartupload.SmartUpload;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;

public class RescueTecService {

	public static RescueTec loadRescueTec(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			String itemId= request.getParameter("itemId");
			if(StringUtils.isEmpty(itemId)) return null;
			RescueTecDAO dao = new RescueTecDAO();
			return dao.find(RescueTec.class, Integer.parseInt(itemId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static List<RescueTec> loadRescueTecList(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			RescueTecDAO dao = new RescueTecDAO();
			List<RescueTec> list = dao.findAll(RescueTec.class);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private static ReturnObject saveRescueTec(
			SmartUpload smartUpload, User user) {
		SmartRequest request = smartUpload.getRequest();
		String[] fileNames = smartUpload.getFileNames();
		ReturnObject ret = new ReturnObject();
		try {
			RescueTec pic = new RescueTec();
			RescueTecDAO dao = new RescueTecDAO();
			String docName = request.getParameter("name");
			pic.setName(docName);
			if(fileNames != null && fileNames.length > 0){
				String storeName = fileNames[0].substring(fileNames[0].lastIndexOf("\\") + 1);
				pic.setStoreName(storeName);
			}
			pic.setCategory(Integer.parseInt(request.getParameter("category")));
			pic.setTimestamp(TimeUtil.showCurrentDay());
			pic.setDescription(request.getParameter("description"));
			dao.add(pic);
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		
		}
		return ret;
	}
	
	private static ReturnObject modifyRescueTec(
			SmartUpload smartUpload, User user){
		SmartRequest request = smartUpload.getRequest();
		String itemId = request.getParameter("itemId");
		String[] fileNames = smartUpload.getFileNames();
		ReturnObject ret = new ReturnObject();
		try {
			RescueTecDAO dao = new RescueTecDAO();
			RescueTec rt = dao.find(RescueTec.class, Integer.parseInt(itemId));
			if(fileNames != null && fileNames.length > 0){
				String storeName = fileNames[0].substring(fileNames[0].lastIndexOf("\\") + 1);
				rt.setStoreName(storeName);
			}
			rt.setTimestamp(TimeUtil.showCurrentDay());
			dao.modify(rt);
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		
		}
		return ret;
	}
	
	public static ReturnObject modifyRescueTec(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int itemId = Integer.parseInt(request.getParameter("itemId"));
			RescueTecDAO dao = new RescueTecDAO();
			RescueTec bean = dao.find(RescueTec.class, itemId);
			bean.setDescription(request.getParameter("description"));
			bean.setName(request.getParameter("name"));
			bean.setCategory(Integer.parseInt(request.getParameter("category")));
			dao.modify(bean);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}
	
	public static ReturnObject removeRescueTec(HttpServletRequest request, String itemId){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			if(itemId == null || "".equals(itemId)){
				itemId = request.getParameter("itemId");
			}
			RescueTecDAO dao = new RescueTecDAO();
			RescueTec rt = dao.find(RescueTec.class, Integer.parseInt(itemId));
			String docName = rt.getStoreName();
			String path = request.getRealPath(Consts.FILE_UPLOAD_SAVE_DIR);
			dao.remove(RescueTec.class, Integer.parseInt(itemId));
			File file = new File(path + "/" + docName);
			file.delete();
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	public static ReturnObject operateRescueTec(
			HttpServletRequest httpRequest, PageContext pageContext) {
		ReturnObject retObj = null;
		try {
			httpRequest.setCharacterEncoding("UTF-8");
			User user = (User)httpRequest.getSession().getAttribute(com.infomanage.util.Consts.LOGINED_USER);
			String actionType = httpRequest.getParameter("actionType");
			if (actionType == null) {
				FileUpload fileUpload = new FileUpload("doc,docx");
				SmartUpload smartUpload = fileUpload.upload(httpRequest,
						pageContext);
				SmartRequest request = smartUpload.getRequest();
				actionType = request.getParameter("actionType");
				if ("add".equals(actionType)) {
					retObj = saveRescueTec(smartUpload, user);
				}
				if ("modify".equals(actionType)) {
					retObj = modifyRescueTec(smartUpload, user);
				}
			}else if("del".equals(actionType)){
				removeRescueTec(httpRequest, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			retObj.setErrorMessage(e.getMessage());
		}
		return retObj;
	}
	
	private static SearchBean buildSearchCondition(HttpServletRequest request, HttpServletResponse response,
			JspWriter out) throws Exception {
		request.setCharacterEncoding("UTF-8");
		String name = request.getParameter("name");
		SearchBean searchBean = new SearchBean();
		if (!StringUtil.isEmpty(name)) {
			FieldBean field = new FieldBean("name", name);
			field.setLike(true);
			searchBean.add(field);
		}
		
		// 设置权限
		User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
		if (user != null) {
			AuthorityBean authority = new AuthorityBean();
			authority.setRole(RoleType.valueOf(user.getRoleId()));
			authority.setDeptId(user.getAssociatedDept());
			searchBean.setAuthority(authority);
		}
		return searchBean;
	}
	
	public static int getPageCount(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			RescueTecDAO dao = new RescueTecDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(searchBean) + PaginationBean.pageSize - 1)/PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}
	
	public static List<RescueTec> search(HttpServletRequest request, HttpServletResponse response, JspWriter out) {
		try {
			request.setCharacterEncoding("UTF-8");
			RescueTecDAO dao = new RescueTecDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			//设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if(currPageNum == null){
				page.setCurrentPageNum(1);
			}else{
				page.setCurrentPageNum(Integer.parseInt(currPageNum));
				
			}
			searchBean.setPage(page);
			List<RescueTec> list = dao.findByFields(searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ReturnObject removeBatchRescueTec(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			RescueTecDAO dao = new RescueTecDAO();
			if(batchIds != null){
				String[] ids = batchIds.split(";");
				for(String id : ids){
					dao.remove(RescueTec.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("删除失败！");
		}
		return ret;
	}
}
